package handler

import (
	"strconv"
	"web_framework/model"
	"web_framework/pkg/constvar"
	"web_framework/pkg/e"
	"web_framework/service"
	"web_framework/storage"
)

//推送下载任务队列响应接口
type DelTaskPushResponseHandle struct {
}

func (delTaskPushResponseHandle DelTaskPushResponseHandle) handleMsg(data model.Request, sn string) (result model.Response) {
	taskIdStr := strconv.FormatInt(data.Data.TaskID,10)
	taskInfoCacheKey := constvar.RouterRedisPrefix + sn + storage.RedisRouterTaskInfoKey + ":" + taskIdStr
	taskStatus,getInfoErr := storage.RedisHGet(taskInfoCacheKey,"status")
	if getInfoErr != nil{
		e.Log("Info", data.Mtype, "_", taskIdStr, "_", "任务详情查询失败")
		return ReturnErr(data.Mid, constvar.MtypeDelTaskTodDownloadRespone, "任务详情查询失败")
	}
	intStatus, _ := strconv.Atoi(taskStatus)

	if intStatus == model.TaskStatusDeleting{
		//TODO   根据下载模块传过来的任务状态，更新任务队列状态
		if err := service.UpdateQueueTaskStatus(data.Data.TaskID, sn, model.TaskStatusDeleted); err != nil {
			return  model.Response{
				Mid:    data.Mid,
				Mtype:  data.Mtype,
				Status: constvar.StatusOk,
			}
		}
	}

	return model.Response{
		Mid:    data.Mid,
		Mtype:  data.Mtype,
		Status: constvar.StatusOk,
	}
}
